﻿using AutoTask.DAL;
using AutoTask.Model;
using RpcCacheClient.Interface;

namespace AutoTask.Collect.Collect
{
    internal class TaskPlanCollect : ITaskPlanCollect
    {
        private IAutoTaskPlanDAL _BasicDAL;
        private ICacheController _Cache;
        public TaskPlanCollect(IAutoTaskPlanDAL dal, ICacheController cache)
        {
            _Cache = cache;
            _BasicDAL = dal;
        }
        public AutoTaskPlan[] Gets(Guid taskId, int verNum)
        {
            string key = string.Join("_", "TaskPlan", taskId.ToString("N"), verNum);
            if (this._Cache.TryGet(key, out AutoTaskPlan[] plan))
            {
                return plan;
            }
            plan = this._BasicDAL.Gets(taskId);
            this._Cache.Set(key, plan, new TimeSpan(1, 0, 0, 0));
            return plan;
        }

        public int StopTaskPlan(TaskPlanResult[] stops)
        {
            if (stops.Length == 0)
            {
                return 0;
            }
            this._BasicDAL.StopTaskPlan(stops);
            return stops.Length;
        }
    }
}
